package com.example.justbuy.action.buyer;

import com.example.justbuy.pojo.*;
import com.example.justbuy.service.AddressMapperService;
import com.example.justbuy.service.OrderMapperService;
import com.example.justbuy.service.ShoppingCartMapperService;
import com.example.justbuy.util.ConvertDateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

@Controller
public class OrderAction {
    @Autowired
    private OrderMapperService orderMapperService;
    @Autowired
    private ShoppingCartMapperService shoppingCartMapperService;
    @Autowired
    private AddressMapperService addressMapperService;

    @RequestMapping("/show_order.htm")
    public String orderOfType(Model md, HttpServletRequest request, HttpServletResponse response){
        User user= (User)request.getSession().getAttribute("user");
        HashMap mp1=new HashMap();
        mp1.put("userid",user.getId());
        mp1.put("orderState","待付款");
        List<Order> listOne= orderMapperService.getPayOfOrderByUserId(mp1);

        HashMap mp2=new HashMap();
        mp2.put("userid",user.getId());
        mp2.put("orderState","已付款");
        List<Order> listTwo= orderMapperService.getPayOfOrderByUserId(mp2);

        HashMap mp3=new HashMap();
        mp3.put("userid",user.getId());
        mp3.put("orderState","已发货");
        List<Order> listThree= orderMapperService.getPayOfOrderByUserId(mp3);

        HashMap mp4=new HashMap();
        mp4.put("userid",user.getId());
        mp4.put("orderState","待评价");
        List<Order> listFour= orderMapperService.getPayOfOrderByUserId(mp4);

        md.addAttribute("listOne",listOne);
        md.addAttribute("listTwo",listTwo);
        md.addAttribute("listThree",listThree);
        md.addAttribute("listFour",listFour);

        List<Order> list= orderMapperService.getAllOfOrderByUserId(user.getId());
        md.addAttribute("list",list);
        return "all_orders.html";
    }
    /*添加订单*/
    @RequestMapping("/add_oneorder.htm")
     public  String  addOrder(String str ,Model md, HttpServletRequest request, HttpServletResponse response){
        User user= (User)request.getSession().getAttribute("user");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
        String[] arr=str.split(",");
        //生成一个订单id
        String orderId=date+user.getId()+ arr.length+(int)(Math.random()*1000);
        for(int a=0;a<arr.length;a++){
            //根据ID查询购物车表中的相关信息。并插入数据到订单表
             int id =Integer.valueOf(arr[a]);
             HashMap map1=new HashMap();
             map1.put("userId",user.getId());
             map1.put("id",id);
            ShoppingCart sc=shoppingCartMapperService.getOneofShoppingCart(map1);
            HashMap map =new HashMap();
            map.put("addTime", ConvertDateUtil.castDate(date));
            map.put("orderId",orderId);
            map.put("goodsId",sc.getGoodsid());
            map.put("storeId",sc.getStoreid());
            map.put("orderNum",sc.getAmount());
            map.put("allPrice",sc.getMoney());
            map.put("orderState","待付款");
            map.put("userId",Integer.valueOf(user.getId()));
            int flag=orderMapperService.addNewOrderById(map);
            //如果添加进入订单表成功后，将删除对应购物车表的信息
            if(flag>0){
                HashMap map2=new HashMap();
                map2.put("userId",Integer.valueOf(user.getId()));
                map2.put("id",id);
                int aaa= shoppingCartMapperService.deleOfShoppingCartByUserIdAndGoodSId(map2);
                if(aaa>0){
                    System.out.println("删除成功！");
                }
            }else{
                System.out.println("对不起！系统出错");
            }
        }
        //默认收货地址为当前订单的收货地址
        HashMap mapaddress=new HashMap();
        mapaddress.put("defaultaddress","是");
        mapaddress.put("userId",Integer.valueOf(user.getId()));
        Address address= addressMapperService.GetDefaultAddress(mapaddress);
        //添加成功后对订单表进行获取，根据orderid 和userid
        HashMap map0=new HashMap();
        map0.put("userId",Integer.valueOf(user.getId()));
        map0.put("orderId",orderId);
        List<Order> list= orderMapperService.getTheOrderByUseridAndOrderId(map0);
        //获取对应orderId的商品的总金额；
        Double Allprice=orderMapperService.getSumOfOrderByOrderAndUserId(map0);
        md.addAttribute("address",address);
        md.addAttribute("orderId",orderId);
        md.addAttribute("list",list);
        md.addAttribute("Allprice",Allprice);
        return  "OrderDetail.html";
     }

     //立即购买的方法
    @RequestMapping("/buynow.htm")
    public  String buyNow(Integer goodsId,String model,Model md, HttpServletRequest request, HttpServletResponse response){
        User user= (User)request.getSession().getAttribute("user");
        Goods goods=orderMapperService.getOneOfGoodsByGoodsId(goodsId);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
        String orderId=date+user.getId()+ 1+(int)(Math.random()*1000);
        HashMap map=new HashMap();
        map.put("orderId",orderId);
        map.put("userId",Integer.valueOf(user.getId()));
        map.put("addTime", ConvertDateUtil.castDate(date));
        map.put("goodsId",goodsId);
        map.put("storeId",goods.getStoreId());
        map.put("orderNum",1);
        map.put("models",model);
        map.put("allPrice",goods.getPrice());
        map.put("orderState","待付款");
        Double Allprice=goods.getPrice();
        int flag=orderMapperService.addNewOrderById(map);
        if(flag>0){
           HashMap map1=new HashMap<>();
           map1.put("userId",Integer.valueOf(user.getId()));
           map1.put("orderId",orderId);
           List<Order> list= orderMapperService.getTheOrderByUseridAndOrderId(map1);
           //System.out.println(list.get(0).getId());
           HashMap mapaddress=new HashMap();
           //默认收货地址为当前订单的收货地址
           mapaddress.put("defaultaddress","是");
           mapaddress.put("userId",user.getId());
           Address address= addressMapperService.GetDefaultAddress(mapaddress);
           md.addAttribute("address",address);
      //    System.out.println(list.get(0).getId());
            md.addAttribute("orderId",orderId);
            md.addAttribute("list",list);
            md.addAttribute("Allprice",Allprice);
            return  "OrderDetail.html";

        }else{
            md.addAttribute("goodsId",goodsId);
            return "goods_info.htm";
        }

    }
    @RequestMapping("/deleone_order")
    public  String  deleOrder(String orderId,String model,Model md, HttpServletRequest request, HttpServletResponse response){
        User user= (User)request.getSession().getAttribute("user");
        int flag =  orderMapperService.deleOrderByOrderId(orderId);
        if(flag>0){
            System.out.println("删除成功！");
            return "redirect:show_order.htm";
        }else{
            return "redirect:myHome.htm";
        }
    }

    @RequestMapping("/paythis_order")
    public String paythis(String orderId,String model,Model md, HttpServletRequest request, HttpServletResponse response){
        User user= (User)request.getSession().getAttribute("user");
        HashMap map=new HashMap();
        map.put("orderId",orderId);
        map.put("userId",user.getId());
        List<Order> list= orderMapperService.getTheInfoOrder(map);
        HashMap mapaddress=new HashMap();
        //默认收货地址为当前订单的收货地址
        mapaddress.put("defaultaddress","是");
        mapaddress.put("userId",user.getId());
        Address address= addressMapperService.GetDefaultAddress(mapaddress);
        HashMap map0=new HashMap();
        map0.put("userId",Integer.valueOf(user.getId()));
        map0.put("orderId",orderId);
        Double Allprice=orderMapperService.getSumOfOrderByOrderAndUserId(map0);
        md.addAttribute("orderId",orderId);
        md.addAttribute("address",address);
        md.addAttribute("orderId",orderId);
        md.addAttribute("list",list);
        md.addAttribute("Allprice",Allprice);
        return "OrderDetail.html";
    }
    //修改订单状态
    @RequestMapping("/changeOne.htm")
    public String changState(String orderId,Model md, HttpServletRequest request, HttpServletResponse response){
        User user= (User)request.getSession().getAttribute("user");
        HashMap map=new HashMap();
        map.put("orderId",orderId);
        map.put("orderState","待评价");
        int flag=orderMapperService.updateStateById(map);
        if(flag>0){
            return "redirect:show_order.htm";
        }else{
            return "redirect:myHome.htm";
        }
    }
    //转发评价
    @RequestMapping("/evaleateF.htm")
    public  String evaleateF(String id,Model md, HttpServletRequest request, HttpServletResponse response){
        User user= (User)request.getSession().getAttribute("user");
        md.addAttribute("id",id);
        md.addAttribute("userId",user.getId());
        return "evaluate.html";
    }
}
